﻿using Kingdee.BOS;
using Kingdee.BOS.Core;
using Kingdee.BOS.Core.Bill;
using Kingdee.BOS.Core.DynamicForm;
using Kingdee.BOS.Core.Interaction;
using Kingdee.BOS.Core.List;
using Kingdee.BOS.Core.Metadata;
using Kingdee.BOS.Core.Metadata.ConvertElement;
using Kingdee.BOS.Core.Metadata.ConvertElement.ServiceArgs;
using Kingdee.BOS.Core.Metadata.FormElement;
using Kingdee.BOS.Core.SqlBuilder;
using Kingdee.BOS.FileServer.Core;
using Kingdee.BOS.JSON;
using Kingdee.BOS.Orm;
using Kingdee.BOS.Orm.DataEntity;
using Kingdee.BOS.ServiceFacade.KDServiceFx;
using Kingdee.BOS.ServiceHelper;
using Kingdee.BOS.Util;
using Kingdee.BOS.Workflow.ServiceHelper;
using Kingdee.K3.SCM.ServiceHelper;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;

namespace SD.MSXZ.K3.ServiceFacade.ServicesStub
{
    public class MSXZService : KDBaseService
    {
        public MSXZService(KDServiceContext context)
        : base(context)
        {
        }
        public bool CheckK3CloudLogin()
        {
            var result = false;

            try
            {
                var ctx = base.KDContext.Session.AppContext;
                if (ctx != null) result = true;
            }
            catch (Exception e)
            {

            }
            return result;
        }
        public DynamicObjectCollection ExecuteDynamicObject(string strSQL, CommandType cmdType, List<SqlParam> paramList)
        {
            var ctx = base.KDContext.Session.AppContext;
            return Kingdee.BOS.ServiceHelper.DBServiceHelper.ExecuteDynamicObject(ctx, strSQL, null, null, cmdType, paramList != null ? paramList.ToArray() : null);
        }

        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject> BySettleGetReceivableAmount(string lSettleOrgIds, long lSettleCustomerId, long lCurrencyId)
        {
            var ret = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject>("", new JSONObject(), true);

            try
            {
                var ctx = base.KDContext.Session.AppContext;

                var hzRptAmount = SaleServiceHelper.GetReceivableAmount(ctx, Convert.ToInt64(lSettleOrgIds), lSettleCustomerId, lCurrencyId);
                ret.Result["ReceivableAmount"] = hzRptAmount * (-1);
            }
            catch (Exception e)
            {
                ret.Successful = false;
                ret.Message = e.Message;
            }

            return ret;
        }
        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject> MTSaveBill(string formId, JSONObject data)
        {
            var result = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject>("", new JSONObject(), true);

            IBillView billView = null;
            var billId = 0L;
            var targetDocumentStatus = "C";
            try
            {
                var ctx = base.KDContext.Session.AppContext;
                if (formId == "AR_RECEIVEBILL")
                {
                    #region 收款单
                    var status = data.GetValue<string>("FDocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("FPAYORGID", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }

                    billId = data.GetValue<Int64>("Id", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");

                    billView.Model.SetValue("FBillNo", data.GetValue<string>("FBillNo", ""));
                    billView.Model.SetValue("FREMARK", data.GetValue<string>("FREMARK", ""));

                    billView.Model.SetValue("FCONTACTUNIT", data.GetValue<Int64>("FCONTACTUNIT", 0L));
                    billView.InvokeFieldUpdateService("FCONTACTUNIT", -1);

                    billView.Model.SetValue("FPAYUNIT", data.GetValue<Int64>("FCONTACTUNIT", 0L));
                    billView.InvokeFieldUpdateService("FPAYUNIT", -1);

                    billView.Model.SetItemValueByNumber("FSETTLETYPEID", "JSFS31_SYS", 0);
                    billView.InvokeFieldUpdateService("FSETTLETYPEID", 0);
                    billView.Model.SetItemValueByNumber("FPURPOSEID", "SFKYT02_SYS", 0);
                    billView.InvokeFieldUpdateService("FPURPOSEID", 0);

                    (billView as IBillViewService).UpdateValue("FRECTOTALAMOUNTFOR", 0, data.GetValue<decimal>("FRECTOTALAMOUNTFOR", 0m));
                    var accountId = data.GetValue<string>("FACCOUNTID", "");
                    if (string.IsNullOrWhiteSpace(accountId) == false)
                    {
                        billView.Model.SetItemValueByNumber("FACCOUNTID", accountId, 0);
                        billView.InvokeFieldUpdateService("FACCOUNTID", 0);
                    }

                    billView.Model.SetValue("FCOMMENT", data.GetValue<string>("FCOMMENT", ""), 0);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                    }
                    else
                    {
                        var errorMsg = "收款单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                    #endregion
                }
                else if (formId == "BSD_CZJLD")
                {
                    #region 餐损返点申请单
                    var status = data.GetValue<string>("FDocumentStatus", "");
                    targetDocumentStatus = status;
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("F_BSD_OrgId", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }

                    billId = data.GetValue<Int64>("Id", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");
                    //
                    billView.Model.SetValue("FCustomerId", data.GetValue<Int64>("FCUSTOMERID", 0L));
                    billView.InvokeFieldUpdateService("FCustomerId", -1);

                    billView.Model.SetValue("FOrderNO", data.GetValue<string>("ForderNO", ""));

                    billView.Model.SetValue("FBSAmount", data.GetValue<decimal>("Fbsamount", 0M));

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        #region 上传图片
                        var pictures = data.GetValue<JSONArray>("FPictures", new JSONArray());
                        if (pictures.Count > 0)
                        {
                            UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "餐损返点申请单图片");
                        }
                        #endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (targetDocumentStatus != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "餐损返点申请单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                    #endregion
                }
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }

            try
            {
                if (billId > 0L && result.Successful == true)
                {
                    result.Result["Id"] = billId;

                    if (targetDocumentStatus == "C")
                    {
                        AuditBill(formId, billId);
                    }
                }
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }

            return result;
        }
        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<object> SZGDPushSaveBill(string formId, JSONObject data)
        {
            var result = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<object>("", 0L, true);
            var ctx = base.KDContext.Session.AppContext;

            IBillView billView = null;
            var sourcebillId = 0L;
            var targetBillId = 0L;
            try
            {

                if (formId == "BDI_GCXM_GCJDQRLY")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_GCHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_工程合同转进度产值内验";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);

                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);

                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目进度产值(内验)保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    //else if (sourcebillId > 0L && targetBillId > 0L)
                    //{
                    //    billView = GetTargetFormView(ctx, formId, targetBillId);
                    //    if (billView == null) throw new Exception("单据视图为空");
                    //    var tbrId = data.GetValue<Int64>("TBRId", 0L);
                    //    var date = data.GetValue<DateTime>("BillDate", DateTime.Now);
                    //    var canEditRate = data.GetValue<bool>("CanEditRate", false);
                    //    var lJJDRate = data.GetValue<decimal>("LJJDRate", 0M);
                    //    var remark = data.GetValue<string>("Remark", "");
                    //    billView.Model.SetValue("FTBR", tbrId, -1);
                    //    billView.InvokeFieldUpdateService("FTBR", -1);
                    //    var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                    //    int i = 0;
                    //    foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                    //    {
                    //        var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                    //        var note = entryRow.GetValue<string>("Note", "");
                    //        billView.Model.SetValue("FQty", qty, i);
                    //        billView.InvokeFieldUpdateService("FQty", i);
                    //        billView.Model.SetValue("FNote", note, i);
                    //        billView.InvokeFieldUpdateService("FNote", i);
                    //        i++;
                    //    }
                    //    var saveOpt = OperateOption.Create();
                    //    saveOpt.SetVariableValue("ValidatePermission", false);
                    //    saveOpt.SetIgnoreWarning(true);
                    //    saveOpt.SetIgnoreScopeValidateFlag(true);
                    //    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    //    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    //    var retSave = billView.Model.Save(saveOpt);
                    //    if (retSave.IsSuccess)
                    //    {
                    //        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                    //        targetBillId = Convert.ToInt64(obj["Id"]);
                    //        result.Result = targetBillId;
                    //        result.Message = string.Format("{0}", targetBillId);
                    //        result.Successful = true;
                    //    }
                    //    else
                    //    {
                    //        var errorMsg = "项目进度产值(内验)保存失败";
                    //        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                    //        {
                    //            errorMsg = retSave.ValidationErrors[0].Message;
                    //        }
                    //        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                    //        {
                    //            errorMsg = retSave.OperateResult[0].Message;
                    //        }

                    //        result.Successful = false;
                    //        result.Message = errorMsg;
                    //    }
                    //}
                }
                else if (formId == "BDI_GCXM_GCJDQR")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_GCJDQRLY";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_项目进度产值内验转外验";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);

                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目进度产值(外验)保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_GCQZ")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_GCHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "d237da25-131e-468a-9368-d0ac1c6f4071";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "工程签证变更单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_CLJCYS")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_CGHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_采购合同材料进场验收";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "材料进场验收单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_XMCLLY")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "STK_InStock";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_采购入库转项目材料领用";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }

                        result.Result = targetBillId;
                        result.Message = string.Format("{0}", targetBillId);
                        result.Successful = true;
                        //billView = GetTargetFormView(ctx, formId, targetBillId);
                        //if (billView == null) throw new Exception("单据视图为空");
                        //var userId = data.GetValue<Int64>("UserId", 0L);
                        //var stockId = data.GetValue<Int64>("StockId", 0L);

                        //billView.Model.SetValue("FCreatorId", userId, -1);
                        //billView.InvokeFieldUpdateService("FCreatorId", -1);

                        //billView.Model.SetValue("FStockID", stockId, -1);
                        //billView.InvokeFieldUpdateService("FStockID", -1);

                        ////var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        ////int i = 0;
                        ////foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        ////{
                        ////    billView.Model.SetValue("FQty", 0M, i);
                        ////    billView.InvokeFieldUpdateService("FQty", i);
                        ////    i++;
                        ////}
                        //var entryRows = billView.Model.DataObject;
                        //var entryRow = entryRows["Entity"] as DynamicObjectCollection;
                        //foreach (var obj in entryRow)
                        //{
                        //    obj["FQty"] = 0M;
                        //}
                        //var saveOpt = OperateOption.Create();
                        //saveOpt.SetVariableValue("ValidatePermission", false);
                        //saveOpt.SetIgnoreWarning(true);
                        //saveOpt.SetIgnoreScopeValidateFlag(true);
                        //saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        //saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        //var retSave = billView.Model.Save(saveOpt);
                        //if (retSave.IsSuccess)
                        //{
                        //    var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        //    targetBillId = Convert.ToInt64(obj["Id"]);
                        //    result.Result = targetBillId;
                        //    result.Message = string.Format("{0}", targetBillId);
                        //    result.Successful = true;
                        //}
                        //else
                        //{
                        //    var errorMsg = "项目材料领用单保存失败";
                        //    if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        //    {
                        //        errorMsg = retSave.ValidationErrors[0].Message;
                        //    }
                        //    else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        //    {
                        //        errorMsg = retSave.OperateResult[0].Message;
                        //    }
                        //    result.Successful = false;
                        //    result.Message = errorMsg;
                        //}
                    }
                }
                else if (formId == "BDI_GCXM_XMCLTH")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_XMCLLY";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_项目材料领用退库";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目材料退库单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_JXSBJC")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_ZLHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_租赁进场登记";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "租赁进场登记单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_JXSBTCDJ")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_JXSBJC";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_租赁结算与归还";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "租赁结算与归还单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_AQJCJL")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_GCHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_工程合同关联安全检查记录";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var aqxmId = GetAQXMId(ctx);
                        if (aqxmId == 0L) throw new Exception("获取不到检查项目");
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var entryRows = billView.Model.DataObject;
                        var entryRow = entryRows["Entity"] as DynamicObjectCollection;
                        foreach (var obj in entryRow)
                        {
                            obj["FJCXM_Id"] = aqxmId;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "安全检查记录单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_ZLJCJL")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_GCHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_工程合同关联质量检查记录";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var zlxmId = GetZLXMId(ctx);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        var entryRows = billView.Model.DataObject;
                        var entryRow = entryRows["Entity"] as DynamicObjectCollection;
                        foreach (var obj in entryRow)
                        {
                            obj["FJCXM_Id"] = zlxmId;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "质量检查记录单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                else if (formId == "BDI_GCXM_JDJHHBD")
                {
                    var status = "";

                    var sourceFormId = data.GetValue<string>("SourceFormId", "");
                    var entryId = data.GetValue<Int64>("EntryId", 0L);
                    var targetFormId = formId;
                    if (sourceFormId == "BDI_GCXM_YJDJHWF")
                    {
                        sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                        targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                        if (sourcebillId > 0L && targetBillId == 0L)
                        {
                            var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                            var lstSelectedRow = new List<ListSelectedRow>();

                            var selRow = new ListSelectedRow(sourcebillId.ToString(), entryId.ToString(), 0, sourceFormId);
                            selRow.EntryEntityKey = "FEntity";
                            lstSelectedRow.Add(selRow);
                            var convertRuleId = "BDI_GCXM_月进度计划汇报";
                            var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                            ConvertRuleElement convertRuleElement = null;
                            if (string.IsNullOrWhiteSpace(convertRuleId))
                            {
                                convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                            }
                            else
                            {
                                convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                            }
                            OperateOption option = OperateOption.Create();
                            option.SetVariableValue("ValidatePermission", false);
                            PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                            pushArgs.SelectedRows = lstSelectedRow.ToArray();
                            //pushArgs.TargetBillTypeId = targetBillTypeId;
                            var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                            var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                            if (targetBillObj == null) throw new Exception("下推失败");
                            var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                            if (draftResult.IsSuccess)
                            {
                                targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                                targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                                status = "Z";
                            }
                            else
                            {
                                status = "";
                                throw new Exception("暂存失败");
                            }
                            billView = GetTargetFormView(ctx, formId, targetBillId);
                            if (billView == null) throw new Exception("单据视图为空");
                            var userId = data.GetValue<Int64>("UserId", 0L);
                            var gcxmId = data.GetValue<Int64>("GCXMId", 0L);

                            billView.Model.SetValue("FCreatorId", userId, -1);
                            billView.InvokeFieldUpdateService("FCreatorId", -1);

                            billView.Model.SetValue("FGCXMID", gcxmId, -1);
                            billView.InvokeFieldUpdateService("FGCXMID", -1);

                            billView.Model.SetValue("FRealStart", DateTime.Now, -1);
                            billView.InvokeFieldUpdateService("FRealStart", -1);

                            var saveOpt = OperateOption.Create();
                            saveOpt.SetVariableValue("ValidatePermission", false);
                            saveOpt.SetIgnoreWarning(true);
                            saveOpt.SetIgnoreScopeValidateFlag(true);
                            saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                            saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                            var retSave = billView.Model.Save(saveOpt);
                            if (retSave.IsSuccess)
                            {
                                var obj = retSave.SuccessDataEnity.FirstOrDefault();
                                targetBillId = Convert.ToInt64(obj["Id"]);
                                result.Result = targetBillId;
                                result.Message = string.Format("{0}", targetBillId);
                                result.Successful = true;
                            }
                            else
                            {
                                var errorMsg = "进度计划汇报单保存失败";
                                if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                                {
                                    errorMsg = retSave.ValidationErrors[0].Message;
                                }
                                else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                                {
                                    errorMsg = retSave.OperateResult[0].Message;
                                }
                                result.Successful = false;
                                result.Message = errorMsg;
                            }
                        }
                    }
                    else
                    {
                        sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                        targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                        if (sourcebillId > 0L && targetBillId == 0L)
                        {
                            var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                            var lstSelectedRow = new List<ListSelectedRow>();

                            var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                            lstSelectedRow.Add(selRow);
                            var convertRuleId = "BDI_GCXM_总进度计划汇报";
                            var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                            ConvertRuleElement convertRuleElement = null;
                            if (string.IsNullOrWhiteSpace(convertRuleId))
                            {
                                convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                            }
                            else
                            {
                                convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                            }
                            OperateOption option = OperateOption.Create();
                            option.SetVariableValue("ValidatePermission", false);
                            PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                            pushArgs.SelectedRows = lstSelectedRow.ToArray();
                            //pushArgs.TargetBillTypeId = targetBillTypeId;
                            var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                            var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                            if (targetBillObj == null) throw new Exception("下推失败");
                            var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                            if (draftResult.IsSuccess)
                            {
                                targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                                targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                                status = "Z";
                            }
                            else
                            {
                                status = "";
                                throw new Exception("暂存失败");
                            }
                            billView = GetTargetFormView(ctx, formId, targetBillId);
                            if (billView == null) throw new Exception("单据视图为空");
                            var userId = data.GetValue<Int64>("UserId", 0L);
                            billView.Model.SetValue("FCreatorId", userId, -1);
                            billView.InvokeFieldUpdateService("FCreatorId", -1);

                            billView.Model.SetValue("FRealStart", DateTime.Now, -1);
                            billView.InvokeFieldUpdateService("FRealStart", -1);

                            var saveOpt = OperateOption.Create();
                            saveOpt.SetVariableValue("ValidatePermission", false);
                            saveOpt.SetIgnoreWarning(true);
                            saveOpt.SetIgnoreScopeValidateFlag(true);
                            saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                            saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                            var retSave = billView.Model.Save(saveOpt);
                            if (retSave.IsSuccess)
                            {
                                var obj = retSave.SuccessDataEnity.FirstOrDefault();
                                targetBillId = Convert.ToInt64(obj["Id"]);
                                result.Result = targetBillId;
                                result.Message = string.Format("{0}", targetBillId);
                                result.Successful = true;
                            }
                            else
                            {
                                var errorMsg = "进度计划汇报单保存失败";
                                if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                                {
                                    errorMsg = retSave.ValidationErrors[0].Message;
                                }
                                else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                                {
                                    errorMsg = retSave.OperateResult[0].Message;
                                }
                                result.Successful = false;
                                result.Message = errorMsg;
                            }
                        }
                    }
                }
                else if (formId == "BDI_GCXM_FBJDQR")
                {
                    var status = "";

                    var targetFormId = formId;
                    var sourceFormId = "BDI_GCXM_FBHT";
                    sourcebillId = data.GetValue<Int64>("SourcebillId", 0L);
                    targetBillId = data.GetValue<Int64>("TargetBillId", 0L);
                    if (sourcebillId > 0L && targetBillId == 0L)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, targetFormId) as FormMetadata;

                        var lstSelectedRow = new List<ListSelectedRow>();

                        var selRow = new ListSelectedRow(sourcebillId.ToString(), "", -1, sourceFormId);
                        lstSelectedRow.Add(selRow);
                        var convertRuleId = "BDI_GCXM_分包合同进度确认";
                        var convertRules = ConvertServiceHelper.GetConvertRules(ctx, sourceFormId, targetFormId);
                        ConvertRuleElement convertRuleElement = null;
                        if (string.IsNullOrWhiteSpace(convertRuleId))
                        {
                            convertRuleElement = convertRules.Where(p => p.Status).FirstOrDefault();
                        }
                        else
                        {
                            convertRuleElement = convertRules.Where(p => p.Id.EqualsIgnoreCase(convertRuleId)).FirstOrDefault();
                        }
                        OperateOption option = OperateOption.Create();
                        option.SetVariableValue("ValidatePermission", false);
                        PushArgs pushArgs = new PushArgs(convertRuleElement, new ListSelectedRow[0]);
                        pushArgs.SelectedRows = lstSelectedRow.ToArray();
                        //pushArgs.TargetBillTypeId = targetBillTypeId;
                        var convertOperationResult = ConvertServiceHelper.Push(ctx, pushArgs, option);
                        var targetBillObj = convertOperationResult.TargetDataEntities.Select(p => p.DataEntity).FirstOrDefault();
                        if (targetBillObj == null) throw new Exception("下推失败");
                        var draftResult = BusinessDataServiceHelper.Draft(ctx, targetFormMatadata.BusinessInfo, targetBillObj, null, "Draft");
                        if (draftResult.IsSuccess)
                        {
                            targetBillObj = draftResult.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(targetBillObj["Id"]);
                            status = "Z";
                        }
                        else
                        {
                            status = "";
                            throw new Exception("暂存失败");
                        }
                        billView = GetTargetFormView(ctx, formId, targetBillId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            targetBillId = Convert.ToInt64(obj["Id"]);
                            result.Result = targetBillId;
                            result.Message = string.Format("{0}", targetBillId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "分包进度确认单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }
                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }

                }
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }
            finally
            {
                if (billView != null)
                {
                    billView.Model.ClearDirty();
                    billView.Model.DataChanged = false;

                    billView.Close();
                }
            }

            try
            {
                if (targetBillId > 0L && result.Successful == false)
                {
                    switch (formId)
                    {
                        case "BDI_GCXM_GCJDQRLY":
                        case "BDI_GCXM_GCJDQR":
                        case "BDI_GCXM_GCQZ":
                        case "BDI_GCXM_CLJCYS":
                        case "BDI_GCXM_XMCLLY":
                        case "BDI_GCXM_XMCLTH":
                        case "BDI_GCXM_JXSBJC":
                        case "BDI_GCXM_JXSBTCDJ":
                        case "BDI_GCXM_AQJCJL":
                        case "BDI_GCXM_ZLJCJL":
                        case "BDI_GCXM_JDJHHBD":
                        case "BDI_GCXM_FBJDQR":
                            var targetFormMatadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
                            BusinessDataServiceHelper.Delete(ctx, targetFormMatadata.BusinessInfo, new object[] { targetBillId }, OperateOption.Create(), "Delete");

                            break;
                    }
                }
            }
            catch (Exception e)
            {
                result.Message = e.Message;
            }

            return result;
        }
        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<object> SZGDReviseSaveSubmitBill(string formId, JSONObject data)
        {
            var result = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<object>("", 0L, true);
            try
            {
                var ctx = base.KDContext.Session.AppContext;

                IBillView billView = null;
                var billId = 0L;
                var documentStatus = data.GetValue<string>("DocumentStatus", "");
                if (documentStatus == "B")
                {
                    throw new Exception("禁止操作：单据状态为审核中");
                }
                else if (documentStatus == "C")
                {
                    throw new Exception("禁止操作：单据状态为已审核");
                }
                try
                {
                    if (formId == "BDI_GCXM_GCJDQRLY")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var canEditRate = data.GetValue<bool>("CanEditRate", false);
                        var lJJDRate = data.GetValue<decimal>("LJJDRate", 0M);
                        var remark = data.GetValue<string>("Remark", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);
                        if (canEditRate == true)
                        {
                            billView.Model.SetValue("FCanEditRate", true, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);

                            billView.Model.SetValue("FLJJDRate", lJJDRate, -1);
                        }
                        else
                        {
                            billView.Model.SetValue("FCanEditRate", false, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);
                        }
                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);
                        billView.Model.SetValue("Remark", remark, -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "项目进度产值(内验)单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目进度产值(内验)保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_GCJDQR")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var canEditRate = data.GetValue<bool>("CanEditRate", false);
                        var lJJDRate = data.GetValue<decimal>("LJJDRate", 0M);
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);
                        if (canEditRate == true)
                        {
                            billView.Model.SetValue("FCanEditRate", true, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);

                            billView.Model.SetValue("FLJJDRate", lJJDRate, -1);
                        }
                        else
                        {
                            billView.Model.SetValue("FCanEditRate", false, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);
                        }
                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "项目进度产值(外验)单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目进度产值(外验)保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_GCQZ")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var amount = data.GetValue<decimal>("Amount", 0M);
                        var qzlr = data.GetValue<string>("QZLR", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FAmount", amount, -1);
                        billView.InvokeFieldUpdateService("FAmount", -1);

                        billView.Model.SetValue("FQZLR", qzlr, -1);
                        billView.InvokeFieldUpdateService("FQZLR", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "工程签证变更单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "工程签证变更单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_CLJCYS")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var ysrId = data.GetValue<Int64>("YSRId", 0L);
                        var stockId = data.GetValue<Int64>("StockId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var billName = data.GetValue<string>("BillName", "");

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FYSR", ysrId, -1);
                        billView.InvokeFieldUpdateService("FYSR", -1);

                        billView.Model.SetValue("FStockID", stockId, -1);
                        billView.InvokeFieldUpdateService("FStockID", -1);

                        billView.Model.SetValue("FBillName", billName, -1);
                        billView.InvokeFieldUpdateService("FBillName", -1);

                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "材料进场验收单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "材料进场验收单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_XMCLLY")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var ysrId = data.GetValue<Int64>("YSRId", 0L);
                        var stockId = data.GetValue<Int64>("StockId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var billName = data.GetValue<string>("BillName", "");

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FYSR", ysrId, -1);
                        billView.InvokeFieldUpdateService("FYSR", -1);

                        billView.Model.SetValue("FStockID", stockId, -1);
                        billView.InvokeFieldUpdateService("FStockID", -1);

                        billView.Model.SetValue("FBillName", billName, -1);
                        billView.InvokeFieldUpdateService("FBillName", -1);


                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "项目材料领用单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目材料领用单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_XMCLTH")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var ysrId = data.GetValue<Int64>("YSRId", 0L);
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var billName = data.GetValue<string>("BillName", "");

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FYSR", ysrId, -1);
                        billView.InvokeFieldUpdateService("FYSR", -1);

                        billView.Model.SetValue("FBillName", billName, -1);
                        billView.InvokeFieldUpdateService("FBillName", -1);

                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "项目材料退库单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "项目材料退库单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_JXSBJC")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var ysrId = data.GetValue<Int64>("YSRId", 0L);
                        var billName = data.GetValue<string>("BillName", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FBillName", billName, -1);
                        billView.InvokeFieldUpdateService("FBillName", -1);

                        billView.Model.SetValue("FYSR", ysrId, -1);
                        billView.InvokeFieldUpdateService("FYSR", -1);

                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var jishou = entryRow.GetValue<string>("JiShou", "");
                            var jxsbQty = entryRow.GetValue<Decimal>("JXSBQty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FJS", jishou, i);
                            billView.InvokeFieldUpdateService("FJS", i);
                            billView.Model.SetValue("FJXSBQty", jxsbQty, i);
                            billView.InvokeFieldUpdateService("FJXSBQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "租赁进场登记单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "租赁进场登记单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_JXSBTCDJ")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var jishou = entryRow.GetValue<string>("JiShou", "");
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var backQty = entryRow.GetValue<Decimal>("BackQty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FJS", jishou, i);
                            billView.InvokeFieldUpdateService("FJS", i);
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FBackQty", backQty, i);
                            billView.InvokeFieldUpdateService("FBackQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "租赁结算与归还单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "租赁结算与归还单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_AQJCJL")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var jclxId = data.GetValue<Int64>("JCLXId", 0L);
                        var jcrId = data.GetValue<Int64>("JCRId", 0L);
                        var jcdw = data.GetValue<string>("JCDW", "");
                        var remark = data.GetValue<string>("Remark", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FJCLX", jclxId, -1);
                        billView.InvokeFieldUpdateService("FJCLX", -1);

                        billView.Model.SetValue("FJCR", jcrId, -1);
                        billView.InvokeFieldUpdateService("FJCR", -1);

                        billView.Model.SetValue("FJCDW", jcdw, -1);
                        billView.InvokeFieldUpdateService("FJCDW", -1);

                        billView.Model.SetValue("FRemark", remark, -1);
                        billView.InvokeFieldUpdateService("FRemark", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var jcxmId = entryRow.GetValue<Int64>("JCXMId", 0L);
                            var jclr = entryRow.GetValue<string>("JCLR", "");
                            var czwt = entryRow.GetValue<string>("CZWT", "");
                            var score = entryRow.GetValue<Decimal>("Score", 0M);

                            billView.Model.SetValue("FJCXM", jcxmId, i);
                            billView.InvokeFieldUpdateService("FJCXM", i);
                            billView.Model.SetValue("FJCLR", jclr, i);
                            billView.InvokeFieldUpdateService("FJCLR", i);
                            billView.Model.SetValue("FCZWT", czwt, i);
                            billView.InvokeFieldUpdateService("FCZWT", i);
                            billView.Model.SetValue("FScore", score, i);
                            billView.InvokeFieldUpdateService("FScore", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "安全检查记录单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "安全检查记录单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_ZLJCJL")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var jclxId = data.GetValue<Int64>("JCLXId", 0L);
                        var jcrId = data.GetValue<Int64>("JCRId", 0L);
                        var jcdw = data.GetValue<string>("JCDW", "");
                        var remark = data.GetValue<string>("Remark", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FJCLX", jclxId, -1);
                        billView.InvokeFieldUpdateService("FJCLX", -1);

                        billView.Model.SetValue("FJCR", jcrId, -1);
                        billView.InvokeFieldUpdateService("FJCR", -1);

                        billView.Model.SetValue("FJCDW", jcdw, -1);
                        billView.InvokeFieldUpdateService("FJCDW", -1);

                        billView.Model.SetValue("FRemark", remark, -1);
                        billView.InvokeFieldUpdateService("FRemark", -1);
                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var jcxmId = entryRow.GetValue<Int64>("JCXMId", 0L);
                            var jclr = entryRow.GetValue<string>("JCLR", "");
                            var czwt = entryRow.GetValue<string>("CZWT", "");
                            var score = entryRow.GetValue<Decimal>("Score", 0M);

                            billView.Model.SetValue("FJCXM", jcxmId, i);
                            billView.InvokeFieldUpdateService("FJCXM", i);
                            billView.Model.SetValue("FJCLR", jclr, i);
                            billView.InvokeFieldUpdateService("FJCLR", i);
                            billView.Model.SetValue("FCZWT", czwt, i);
                            billView.InvokeFieldUpdateService("FCZWT", i);
                            billView.Model.SetValue("FScore", score, i);
                            billView.InvokeFieldUpdateService("FScore", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "质量检查记录单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "质量检查记录单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_JDJHHBD")
                    {

                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var realStart = data.GetValue<DateTime>("RealStart", DateTime.Now);
                        var start = data.GetValue<DateTime>("Start", DateTime.Now);
                        var finish = data.GetValue<DateTime>("Finish", DateTime.Now);
                        var curRate = data.GetValue<decimal>("CurRate", 0M);
                        var allRate = data.GetValue<decimal>("AllRate", 0M);
                        var isCompleted = data.GetValue<bool>("IsCompleted", false);
                        var note = data.GetValue<string>("Note", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FRealStart", realStart, -1);
                        billView.InvokeFieldUpdateService("FRealStart", -1);

                        billView.Model.SetValue("FStart", start, -1);
                        billView.InvokeFieldUpdateService("FStart", -1);

                        billView.Model.SetValue("FFinish", finish, -1);
                        billView.InvokeFieldUpdateService("FFinish", -1);

                        billView.Model.SetValue("FCurRate", curRate, -1);
                        billView.InvokeFieldUpdateService("FCurRate", -1);

                        //billView.Model.SetValue("FAllRate", allRate, -1);
                        //billView.InvokeFieldUpdateService("FAllRate", -1);

                        billView.Model.SetValue("FIsCompleted", isCompleted, -1);
                        billView.InvokeFieldUpdateService("FIsCompleted", -1);

                        billView.Model.SetValue("FNote", note, -1);
                        billView.InvokeFieldUpdateService("FNote", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "进度计划汇报单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "进度计划汇报单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_FBJDQR")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var canEditRate = data.GetValue<bool>("CanEditRate", false);
                        var lJJDRate = data.GetValue<decimal>("LJJDRate", 0M);
                        var remark = data.GetValue<string>("Remark", "");
                        var userId = data.GetValue<Int64>("UserId", 0L);

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);
                        if (canEditRate == true)
                        {
                            billView.Model.SetValue("FCanEditRate", true, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);

                            billView.Model.SetValue("FLJJDRate", lJJDRate, -1);
                            billView.InvokeFieldUpdateService("FLJJDRate", -1);
                        }
                        else
                        {
                            billView.Model.SetValue("FCanEditRate", false, -1);
                            billView.InvokeFieldUpdateService("FCanEditRate", -1);
                        }
                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);

                        billView.Model.SetValue("FRemark", remark, -1);
                        billView.InvokeFieldUpdateService("FRemark", -1);

                        var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                        int i = 0;
                        foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                        {
                            var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                            var note = entryRow.GetValue<string>("Note", "");
                            billView.Model.SetValue("FQty", qty, i);
                            billView.InvokeFieldUpdateService("FQty", i);
                            billView.Model.SetValue("FNote", note, i);
                            billView.InvokeFieldUpdateService("FNote", i);
                            i++;
                        }
                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "分包进度确认单图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "分包进度确认单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_SGRZ")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var gcxmId = data.GetValue<Int64>("GCXMId", 0L);
                        var xmjdzt = data.GetValue<Int64>("XMJDZTId", 0L);
                        if (gcxmId == 0L) throw new Exception("工程项目不允许为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var sgbzName = data.GetValue<string>("SGBZName", "");
                        var jlr = data.GetValue<string>("JLR", "");
                        var tqzk1 = data.GetValue<string>("TQZK1", "");
                        var fl1 = data.GetValue<string>("FL1", "");
                        var qw1 = data.GetValue<string>("QW1", "");
                        var tqzk2 = data.GetValue<string>("TQZK2", "");
                        var fl2 = data.GetValue<string>("FL2", "");
                        var qw2 = data.GetValue<string>("QW2", "");
                        var scqkjl = data.GetValue<string>("SCQKJL", "");
                        var zlaqjl = data.GetValue<string>("ZLAQJL", "");
                        var clgpjjc = data.GetValue<string>("CLGPJJC", "");
                        var remark = data.GetValue<string>("Remark", "");

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FXMJDZT", xmjdzt, -1);
                        billView.InvokeFieldUpdateService("FXMJDZT", -1);

                        //billView.Model.SetValue("FCreatorId", userId, -1);
                        //billView.InvokeFieldUpdateService("FCreatorId", -1);

                        billView.Model.SetValue("FGCXMNumber", gcxmId, -1);
                        billView.InvokeFieldUpdateService("FGCXMNumber", -1);

                        billView.Model.SetValue("FSGBZName", sgbzName, -1);
                        billView.Model.SetValue("FJLR", jlr, -1);
                        billView.Model.SetValue("FTQZK1", tqzk1, -1);
                        billView.Model.SetValue("FFL1", fl1, -1);
                        billView.Model.SetValue("FQW1", qw1, -1);
                        billView.Model.SetValue("FTQZK2", tqzk2, -1);
                        billView.Model.SetValue("FFL2", fl2, -1);
                        billView.Model.SetValue("FQW2", qw2, -1);
                        billView.Model.SetValue("FSCQKJL", scqkjl, -1);
                        billView.Model.SetValue("FZLAQJL", zlaqjl, -1);
                        billView.Model.SetValue("FCLGPJJC", clgpjjc, -1);
                        billView.Model.SetValue("FRemark", remark, -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);
                            #region 上传图片
                            var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                            if (pictures.Count > 0)
                            {
                                UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                            }
                            #endregion

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "施工日志保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                    else if (formId == "BDI_GCXM_XXJD")
                    {
                        billId = data.GetValue<Int64>("BillId", 0L);
                        billView = GetTargetFormView(ctx, formId, billId);
                        if (billView == null) throw new Exception("单据视图为空");
                        var userId = data.GetValue<Int64>("UserId", 0L);
                        var gcxmId = data.GetValue<Int64>("GCXMId", 0L);
                        if (gcxmId == 0L) throw new Exception("工程项目不允许为空");
                        var date = data.GetValue<DateTime>("Date", DateTime.Now);
                        var supplierId = data.GetValue<Int64>("SupplierId", 0L);
                        var tbrId = data.GetValue<Int64>("TBRId", 0L);
                        var ljjdRate = data.GetValue<decimal>("LJJDRate", 0M);
                        var remark = data.GetValue<string>("Remark", "");
                        var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        var picture = "";
                        if (pictures.Count > 0)
                        {
                            picture = Convert.ToString(pictures[0]);
                        }

                        billView.Model.SetValue("FDate", date, -1);
                        billView.InvokeFieldUpdateService("FDate", -1);

                        billView.Model.SetValue("FGCXMID", gcxmId, -1);
                        billView.InvokeFieldUpdateService("FGCXMID", -1);

                        billView.Model.SetValue("FSupplierID", supplierId, -1);
                        billView.InvokeFieldUpdateService("FSupplierID", -1);

                        billView.Model.SetValue("FTBR", tbrId, -1);
                        billView.InvokeFieldUpdateService("FTBR", -1);

                        billView.Model.SetValue("FJDRate", ljjdRate, -1);
                        billView.InvokeFieldUpdateService("FJDRate", -1);

                        billView.Model.SetValue("FCreatorId", userId, -1);
                        billView.InvokeFieldUpdateService("FCreatorId", -1);

                        billView.Model.SetValue("FRemark", remark, -1);

                        billView.Model.SetValue("FImage", picture, -1);
                        billView.InvokeFieldUpdateService("FImage", -1);

                        var saveOpt = OperateOption.Create();
                        saveOpt.SetVariableValue("ValidatePermission", false);
                        saveOpt.SetIgnoreWarning(true);
                        saveOpt.SetIgnoreScopeValidateFlag(true);
                        saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                        saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                        var retSave = billView.Model.Save(saveOpt);
                        if (retSave.IsSuccess)
                        {
                            var obj = retSave.SuccessDataEnity.FirstOrDefault();
                            billId = Convert.ToInt64(obj["Id"]);
                            var billNO = Convert.ToString(obj["BillNO"]);

                            result.Result = billId;
                            result.Message = string.Format("{0}", billId);
                            result.Successful = true;
                        }
                        else
                        {
                            var errorMsg = "形象进度单保存失败";
                            if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                            {
                                errorMsg = retSave.ValidationErrors[0].Message;
                            }
                            else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                            {
                                errorMsg = retSave.OperateResult[0].Message;
                            }

                            result.Successful = false;
                            result.Message = errorMsg;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.Successful = false;
                    result.Message = e.Message;
                }
                finally
                {
                    if (billView != null)
                    {
                        billView.Model.ClearDirty();
                        billView.Model.DataChanged = false;

                        billView.Close();
                    }
                }
                try
                {
                    if (billId > 0L && result.Successful == false)
                    {
                        switch (formId)
                        {
                            //case "BDI_GCXM_GCJDQRLY":
                            //case "BDI_GCXM_GCJDQR":
                            //case "BDI_GCXM_GCQZ":
                            //case "BDI_GCXM_CLJCYS":
                            case "BDI_GCXM_XMCLLY":
                            //case "BDI_GCXM_XMCLTH":
                            case "BDI_GCXM_JXSBJC":
                                //case "BDI_GCXM_JXSBTCDJ":
                                //case "BDI_GCXM_AQJCJL":
                                //case "BDI_GCXM_ZLJCJL":
                                //case "BDI_GCXM_JDJHHBD":
                                //case "BDI_GCXM_FBJDQR":
                                var targetFormMatadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
                                BusinessDataServiceHelper.Delete(ctx, targetFormMatadata.BusinessInfo, new object[] { billId }, OperateOption.Create(), "Delete");

                                break;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.Message = e.Message;
                }

                try
                {
                    result.Result = billId;
                    if (result.Successful == true)
                    {
                        var targetFormMatadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;

                        // 首先判断单据是否已经有未完成的工作流
                        bool isExist = ProcManageServiceHelper.CheckUnCompletePrcInstExsit(ctx, formId, billId.ToString());
                        if (isExist == true)
                        {
                            throw new KDBusinessException("AutoSubmit-001", "该单据已经启动了流程，不允许重复提交！");
                        }

                        // 设置提交参数：忽略操作过程中的警告，避免与用户交互
                        OperateOption submitOption = OperateOption.Create();
                        submitOption.SetIgnoreWarning(true);
                        submitOption.SetIgnoreScopeValidateFlag(true);
                        submitOption.SetVariableValue("ValidatePermission", false);
                        IOperationResult submitResult = null;

                        // 读取单据的工作流配置模板
                        var findProcResultList = WorkflowTemplateServiceHelper.GetPrcListByFormID(formId, new string[] { billId.ToString() }, ctx);
                        if (findProcResultList == null || findProcResultList.Count == 0)
                        {
                            var item = new Kingdee.BOS.Workflow.Models.Template.FindPrcResult();
                            item.Result = Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.OnlySubmit;
                            findProcResultList = new List<Kingdee.BOS.Workflow.Models.Template.FindPrcResult>() { item };
                        }

                        var findProcResult = findProcResultList.FirstOrDefault();
                        if (findProcResult.Result == Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.Error)
                        {
                            throw new KDBusinessException("AutoSubmit-003", "单据不符合流程启动条件，不允许提交工作流！");
                        }
                        else if (findProcResult.Result != Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.Normal)
                        {
                            // 本单无适用的流程图，直接走传统审批
                            submitResult = BusinessDataServiceHelper.Submit(ctx, targetFormMatadata.BusinessInfo, new object[] { billId }, "Submit", submitOption);
                        }
                        else
                        {
                            // 走工作流 
                            submitResult = BOSWorkflowServiceHelper.ListSubmit(ctx, targetFormMatadata.BusinessInfo, 0, new object[] { billId }, findProcResultList, submitOption);
                        }

                        // 判断提交结果
                        if (submitResult.IsSuccess == true)
                        {
                            // 自动提交成功，显示空操作本身的成功提示即可
                        }
                        else
                        {
                            submitResult.MergeValidateErrors();
                            if (submitResult.OperateResult == null)
                            {
                                // 未知原因导致提交失败
                                throw new KDBusinessException("AutoSubmit-004", "未知原因导致自动提交失败！");
                            }
                            else
                            {
                                StringBuilder sb = new StringBuilder();
                                sb.AppendLine("自动提交失败，失败原因：");
                                foreach (var operateResult in submitResult.OperateResult)
                                {
                                    sb.AppendLine(operateResult.Message);
                                }
                                throw new KDBusinessException("AutoSubmit-005", sb.ToString());
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    result.Message = e.Message;
                }
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
                Kingdee.BOS.Log.Logger.Error("金蝶云星空", "失败：", e);
            }
            return result;
        }
        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject> SZGDNewSaveSubmitBill(string formId, JSONObject data)
        {
            var result = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo<JSONObject>("", new JSONObject(), true);
            IBillView billView = null;
            var billId = 0L;
            var status = "";
            try
            {
                var ctx = base.KDContext.Session.AppContext;
                if (formId == "BSD_CZJLD")
                {
                    //var a = 1;
                    //if (a == 1)
                    //{
                    //    throw new Exception("错误");
                    //}
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("OrgId", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");
                    var userId = data.GetValue<Int64>("UserId", 0L);

                    var ygNumber = data.GetValue<string>("F_BSD_STAFF", "");
                    //if (ygId == 0L) throw new Exception("员工不允许为空");
                    var ygphone = data.GetValue<string>("F_BSD_PHONE", "");
                    var czje = data.GetValue<decimal>("F_BSD_JINE", 0M);
                    var czdate = data.GetValue<DateTime>("F_BSD_CZDate", DateTime.Now);

                    billView.Model.SetItemValueByNumber("F_BSD_STAFF", ygNumber, 0);
                    billView.InvokeFieldUpdateService("F_BSD_STAFF", 0);

                    billView.Model.SetValue("F_BSD_PHONE", ygphone, 0);
                    billView.InvokeFieldUpdateService("F_BSD_PHONE", 0);

                    billView.Model.SetValue("F_BSD_JINE", czje, 0);
                    billView.InvokeFieldUpdateService("F_BSD_JINE", 0);

                    billView.Model.SetValue("FCreatorId", userId, -1);
                    billView.InvokeFieldUpdateService("FCreatorId", -1);

                    billView.Model.SetValue("F_BSD_CZDate", czdate, 0);
                    billView.InvokeFieldUpdateService("F_BSD_CZDate", 0);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "充值记录单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }
                else if (formId == "BSD_XFJLD")
                {
                    //var a = 1;
                    //if (a == 1)
                    //{
                    //    throw new Exception("错误");
                    //}
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("OrgId", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }

                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");

                    var userId = data.GetValue<Int64>("UserId", 0L);

                    var ygNumber = data.GetValue<string>("F_BSD_STAFF", "");

                    var phone = data.GetValue<string>("F_BSD_PHONE", "");
                    if (phone == "") throw new Exception("手机号不允许为空");

                    var cpNumber = data.GetValue<string>("F_BSD_CPNUMBER", "");

                    var cpsl = data.GetValue<Int64>("F_BSD_CPSL", 0L);

                    var bz = data.GetValue<string>("F_BSD_BZ", "");

                    billView.Model.SetItemValueByNumber("F_BSD_STAFF", ygNumber, 0);
                    billView.InvokeFieldUpdateService("F_BSD_STAFF", 0);

                    billView.Model.SetValue("F_BSD_PHONE", phone, 0);
                    billView.InvokeFieldUpdateService("F_BSD_PHONE", -1);

                    billView.Model.SetItemValueByNumber("F_BSD_CPNUMBER", cpNumber, 0);
                    billView.InvokeFieldUpdateService("F_BSD_CPNUMBER", 0);

                    billView.Model.SetValue("F_BSD_CPSL", cpsl, 0);
                    billView.InvokeFieldUpdateService("F_BSD_CPSL", 0);

                    billView.Model.SetValue("F_BSD_BZ", bz, 0);
                    billView.InvokeFieldUpdateService("F_BSD_BZ", 0);

                    billView.Model.SetValue("FCreatorId", userId, -1);
                    billView.InvokeFieldUpdateService("FCreatorId", -1);


                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "消费记录单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }
                else if (formId == "BSD_WLRYYCJLD")
                {
                    //var a = 1;
                    //if (a == 1)
                    //{
                    //    throw new Exception("错误");
                    //}
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    //var orgId = data.GetValue<Int64>("OrgId", 1L);

                    //if (ctx.CurrentOrganizationInfo.ID != orgId)
                    //{
                    //    ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    //}
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");


                    var ryxm = data.GetValue<string>("F_SD_XM", "");
                    var ryphone = data.GetValue<string>("F_SD_SJH", "");
                    var sxdate = data.GetValue<DateTime>("F_SD_SXRQ", DateTime.Now);
                    var shxdate = data.GetValue<DateTime>("F_SD_SHXRQ", DateTime.Now);

                    billView.Model.SetValue("F_SD_XM", ryxm, -1);
                    billView.InvokeFieldUpdateService("F_SD_XM", -1);

                    billView.Model.SetValue("F_SD_SJH", ryphone, -1);
                    billView.InvokeFieldUpdateService("F_SD_SJH", -1);

                    billView.Model.SetValue("F_SD_SXRQ", sxdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_SXRQ", -1);

                    billView.Model.SetValue("F_SD_SHXRQ", shxdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_SHXRQ", -1);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "外来人员用餐记录单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }

                else if (formId == "BSD_FKDJD")
                {
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("F_SD_ZZ", 1L);

                    //if (ctx.CurrentOrganizationInfo.ID != orgId)
                    //{
                    //    ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    //}
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");


                    var sqr = data.GetValue<string>("F_SD_SQR", "");
                    var yg = data.GetValue<string>("F_SD_YG", "");
                    var yglxdh = data.GetValue<string>("F_SD_LXDH", "");
                    var bfsx = data.GetValue<string>("F_SD_BFSQ", "");
                    var qsdate = data.GetValue<DateTime>("F_SD_QSRQ", DateTime.Now);
                    var jsdate = data.GetValue<DateTime>("F_SD_JSRQ", DateTime.Now);
                    var ywfl = data.GetValue<Int64>("F_SD_YWFL", 0L);
                    var clry = data.GetValue<string>("F_SD_CL", "");
                    var bfdd = data.GetValue<string>("F_BFDD", "");

                    //var ryxm = data.GetValue<string>("F_SD_RYXM", "");
                    //var zjlx = data.GetValue<Int64>("F_SD_ZJLX", 0L);
                    //var zjhm = data.GetValue<string>("F_SD_ZJHM", "");
                    //var wlrylxdh = data.GetValue<string>("F_SD_LXDHH", "");
                    //var rs = data.GetValue<Int64>("F_SD_RS", 0L);
                    //var cph = data.GetValue<string>("F_SD_CPH", "");
                    //var bfsy = data.GetValue<string>("F_SD_BFSY", "");
                    //var clrc = data.GetValue<string>("F_SD_CLRC", "");
                    //var cz = data.GetValue<string>("F_SD_CZ", "");
                    //var bz = data.GetValue<string>("F_SD_BZ", "");

                    //   var entry = data.GetValue<DynamicObjectCollection>("entry");
                    //var entrys = data.GetValue<Kingdee.BOS.JSON.JSONArray>("entry", new Kingdee.BOS.JSON.JSONArray());


                    billView.Model.SetValue("F_SD_SQR", sqr, -1);
                    billView.InvokeFieldUpdateService("F_SD_SQR", -1);

                    billView.Model.SetItemValueByNumber("F_SD_YG", yg, -1);
                    billView.InvokeFieldUpdateService("F_SD_YG", -1);

                    billView.Model.SetValue("F_SD_LXDH", yglxdh, -1);
                    billView.InvokeFieldUpdateService("F_SD_LXDH", -1);

                    billView.Model.SetValue("F_SD_BFSQ", bfsx, -1);
                    billView.InvokeFieldUpdateService("F_SD_BFSQ", -1);

                    billView.Model.SetValue("F_SD_QSRQ", qsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_QSRQ", -1);

                    billView.Model.SetValue("F_SD_JSRQ", jsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_JSRQ", -1);

                    billView.Model.SetValue("F_SD_YWFL", ywfl, -1);
                    billView.InvokeFieldUpdateService("F_SD_YWFL", -1);

                    billView.Model.SetItemValueByNumber("F_SD_CL", clry, -1);
                    billView.InvokeFieldUpdateService("F_SD_CL", -1);

                    billView.Model.SetValue("F_BFDD", bfdd, -1);
                    billView.InvokeFieldUpdateService("F_BFDD", -1);

                    var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                    int i = 0;
                    foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                    {
                        var ryxm = entryRow.GetValue<string>("F_SD_RYXM", "");
                        var zjlx = entryRow.GetValue<Int64>("F_SD_ZJLX", 0L);
                        var zjhm = entryRow.GetValue<string>("F_SD_ZJHM", "");
                        var wlrylxdh = entryRow.GetValue<string>("F_SD_LXDHH", "");
                        var rs = entryRow.GetValue<Int64>("F_SD_RS", 0L);
                        var cph = entryRow.GetValue<string>("F_SD_CPH", "");
                        var bfsy = entryRow.GetValue<string>("F_SD_BFSY", "");
                        var clrc = entryRow.GetValue<string>("F_SD_CLRC", "");
                        var cz = entryRow.GetValue<string>("F_SD_CZ", "");
                        var bz = entryRow.GetValue<string>("F_SD_BZ", "");

                        //var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                        //var note = entryRow.GetValue<string>("Note", "");
                        billView.Model.SetValue("F_SD_RYXM", ryxm, i);
                        billView.InvokeFieldUpdateService("F_SD_RYXM", i);
                        billView.Model.SetValue("F_SD_ZJLX", zjlx, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJLX", i);
                        billView.Model.SetValue("F_SD_ZJHM", zjhm, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJHM", i);
                        billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, i);
                        billView.InvokeFieldUpdateService("F_SD_LXDHH", i);
                        billView.Model.SetValue("F_SD_RS", rs, i);
                        billView.InvokeFieldUpdateService("F_SD_RS", i);
                        billView.Model.SetValue("F_SD_CPH", cph, i);
                        billView.InvokeFieldUpdateService("F_SD_CPH", i);
                        billView.Model.SetValue("F_SD_BFSY", bfsy, i);
                        billView.InvokeFieldUpdateService("F_SD_BFSY", i);
                        billView.Model.SetValue("F_SD_CLRC", clrc, i);
                        billView.InvokeFieldUpdateService("F_SD_CLRC", i);
                        billView.Model.SetValue("F_SD_CZ", cz, i);
                        billView.InvokeFieldUpdateService("F_SD_CZ", i);
                        billView.Model.SetValue("F_SD_BZ", bz, i);
                        billView.InvokeFieldUpdateService("F_SD_BZ", i);
                        i++;

                        billView.Model.CreateNewEntryRow("FEntity");
                    }
                    //int i = 0;
                    //foreach (JSONObject entry in entrys)
                    //{

                    //    billView.Model.SetValue("F_SD_RYXM", entry["F_SD_RYXM"], i);
                    //    billView.Model.SetValue("F_SD_ZJLX", entry["F_SD_ZJLX"], i);
                    //    billView.Model.SetValue("F_SD_ZJHM", entry["F_SD_ZJHM"], i);
                    //    billView.Model.SetValue("F_SD_LXDHH", entry["F_SD_LXDHH"], i);
                    //    billView.Model.SetValue("F_SD_RS", entry["F_SD_RS"], i);
                    //    billView.Model.SetValue("F_SD_CPH", entry["F_SD_CPH"], i);
                    //    billView.Model.SetValue("F_SD_BFSY", entry["F_SD_BFSY"], i);
                    //    billView.Model.SetValue("F_SD_CLRC", entry["F_SD_CLRC"], i);
                    //    billView.Model.SetValue("F_SD_CZ", entry["F_SD_CZ"], i);
                    //    billView.Model.SetValue("F_SD_BZ", entry["F_SD_BZ"], i);
                    //    i++;

                    //    billView.Model.CreateNewEntryRow("FEntity");
                    //}


                    /////////////////////////////////////




                    //billView.Model.SetValue("F_SD_RYXM", ryxm, 0);
                    //billView.Model.SetValue("F_SD_ZJLX", zjlx, 0);
                    //billView.Model.SetValue("F_SD_ZJHM", zjhm, 0);
                    //billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, 0);
                    //billView.Model.SetValue("F_SD_RS", rs, 0);
                    //billView.Model.SetValue("F_SD_CPH", cph, 0);
                    //billView.Model.SetValue("F_SD_BFSY", bfsy, 0);
                    //billView.Model.SetValue("F_SD_CLRC", clrc, 0);
                    //billView.Model.SetValue("F_SD_CZ", cz, 0);
                    //billView.Model.SetValue("F_SD_BZ", bz, 0);
                    ////////////////////////////////////////////////
                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "访客登记单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }



                else if (formId == "BSD_RCJLD")
                {
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("F_SD_ZZ", 1L);

                    //if (ctx.CurrentOrganizationInfo.ID != orgId)
                    //{
                    //    ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    //}
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");


                    var sqr = data.GetValue<string>("F_SD_SQR", "");
                    var yg = data.GetValue<string>("F_SD_YG", "");
                    var yglxdh = data.GetValue<string>("F_SD_LXDH", "");
                    var bfsx = data.GetValue<string>("F_SD_BFSQ", "");
                    var qsdate = data.GetValue<DateTime>("F_SD_QSRQ", DateTime.Now);
                    var jsdate = data.GetValue<DateTime>("F_SD_JSRQ", DateTime.Now);

                    //var ryxm = data.GetValue<string>("F_SD_RYXM", "");
                    //var zjlx = data.GetValue<Int64>("F_SD_ZJLX", 0L);
                    //var zjhm = data.GetValue<string>("F_SD_ZJHM", "");
                    //var wlrylxdh = data.GetValue<string>("F_SD_LXDHH", "");
                    //var rs = data.GetValue<Int64>("F_SD_RS", 0L);
                    //var cph = data.GetValue<string>("F_SD_CPH", "");
                    //var bfsy = data.GetValue<string>("F_SD_BFSY", "");
                    //var clrc = data.GetValue<string>("F_SD_CLRC", "");
                    //var cz = data.GetValue<string>("F_SD_CZ", "");
                    //var rylx = data.GetValue<Int64>("F_SD_RYLX", 0L);
                    //var bz = data.GetValue<string>("F_SD_BZ", "");


                    billView.Model.SetValue("F_SD_SQR", sqr, -1);
                    billView.InvokeFieldUpdateService("F_SD_SQR", -1);

                    billView.Model.SetItemValueByNumber("F_SD_YG", yg, -1);
                    billView.InvokeFieldUpdateService("F_SD_YG", -1);

                    billView.Model.SetValue("F_SD_LXDH", yglxdh, -1);
                    billView.InvokeFieldUpdateService("F_SD_LXDH", -1);

                    billView.Model.SetValue("F_SD_BFSQ", bfsx, -1);
                    billView.InvokeFieldUpdateService("F_SD_BFSQ", -1);

                    billView.Model.SetValue("F_SD_QSRQ", qsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_QSRQ", -1);

                    billView.Model.SetValue("F_SD_JSRQ", jsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_JSRQ", -1);

                    var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                    int i = 0;
                    foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                    {
                        var ryxm = entryRow.GetValue<string>("F_SD_RYXM", "");
                        var zjlx = entryRow.GetValue<Int64>("F_SD_ZJLX", 0L);
                        var zjhm = entryRow.GetValue<string>("F_SD_ZJHM", "");
                        var wlrylxdh = entryRow.GetValue<string>("F_SD_LXDHH", "");
                        var rs = entryRow.GetValue<Int64>("F_SD_RS", 0L);
                        var cph = entryRow.GetValue<string>("F_SD_CPH", "");
                        var bfsy = entryRow.GetValue<string>("F_SD_BFSY", "");
                        var clrc = entryRow.GetValue<string>("F_SD_CLRC", "");
                        var cz = entryRow.GetValue<string>("F_SD_CZ", "");
                        var rylx = data.GetValue<Int64>("F_SD_RYLX", 0L);
                        var bz = entryRow.GetValue<string>("F_SD_BZ", "");

                        //var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                        //var note = entryRow.GetValue<string>("Note", "");
                        billView.Model.SetValue("F_SD_RYXM", ryxm, i);
                        billView.InvokeFieldUpdateService("F_SD_RYXM", i);
                        billView.Model.SetValue("F_SD_ZJLX", zjlx, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJLX", i);
                        billView.Model.SetValue("F_SD_ZJHM", zjhm, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJHM", i);
                        billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, i);
                        billView.InvokeFieldUpdateService("F_SD_LXDHH", i);
                        billView.Model.SetValue("F_SD_RS", rs, i);
                        billView.InvokeFieldUpdateService("F_SD_RS", i);
                        billView.Model.SetValue("F_SD_CPH", cph, i);
                        billView.InvokeFieldUpdateService("F_SD_CPH", i);
                        billView.Model.SetValue("F_SD_BFSY", bfsy, i);
                        billView.InvokeFieldUpdateService("F_SD_BFSY", i);
                        billView.Model.SetValue("F_SD_CLRC", clrc, i);
                        billView.InvokeFieldUpdateService("F_SD_CLRC", i);
                        billView.Model.SetValue("F_SD_CZ", cz, i);
                        billView.InvokeFieldUpdateService("F_SD_CZ", i);
                        billView.Model.SetValue("F_SD_RYLX", rylx, i);
                        billView.InvokeFieldUpdateService("F_SD_RYLX", i);
                        billView.Model.SetValue("F_SD_BZ", bz, i);
                        billView.InvokeFieldUpdateService("F_SD_BZ", i);
                        i++;

                        billView.Model.CreateNewEntryRow("FEntity");
                    }

                    //billView.Model.SetValue("F_SD_RYXM", ryxm, 0);
                    //billView.Model.SetValue("F_SD_ZJLX", zjlx, 0);
                    //billView.Model.SetValue("F_SD_ZJHM", zjhm, 0);
                    //billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, 0);
                    //billView.Model.SetValue("F_SD_RS", rs, 0);
                    //billView.Model.SetValue("F_SD_CPH", cph, 0);
                    //billView.Model.SetValue("F_SD_BFSY", bfsy, 0);
                    //billView.Model.SetValue("F_SD_CLRC", clrc, 0);
                    //billView.Model.SetValue("F_SD_CZ", cz, 0);
                    //billView.Model.SetValue("F_SD_RYLX", rylx, 0);
                    //billView.Model.SetValue("F_SD_BZ", bz, 0);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "入场记录单单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }


                else if (formId == "BSD_CCJLD")
                {
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("F_SD_ZZ", 1L);

                    //if (ctx.CurrentOrganizationInfo.ID != orgId)
                    //{
                    //    ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    //}
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");


                    var sqr = data.GetValue<string>("F_SD_SQR", "");
                    var yg = data.GetValue<string>("F_SD_YG", "");
                    var yglxdh = data.GetValue<string>("F_SD_LXDH", "");
                    var bfsx = data.GetValue<string>("F_SD_BFSQ", "");
                    var qsdate = data.GetValue<DateTime>("F_SD_QSRQ", DateTime.Now);
                    var jsdate = data.GetValue<DateTime>("F_SD_JSRQ", DateTime.Now);

                    //var ryxm = data.GetValue<string>("F_SD_RYXM", "");
                    //var zjlx = data.GetValue<Int64>("F_SD_ZJLX", 0L);
                    //var zjhm = data.GetValue<string>("F_SD_ZJHM", "");
                    //var wlrylxdh = data.GetValue<string>("F_SD_LXDHH", "");
                    //var rs = data.GetValue<Int64>("F_SD_RS", 0L);
                    //var cph = data.GetValue<string>("F_SD_CPH", "");
                    //var bfsy = data.GetValue<string>("F_SD_BFSY", "");
                    //var clrc = data.GetValue<string>("F_SD_CLRC", "");
                    //var cz = data.GetValue<string>("F_SD_CZ", "");
                    //var rylx = data.GetValue<Int64>("F_SD_RYLX", 0L);
                    //var bz = data.GetValue<string>("F_SD_BZ", "");


                    billView.Model.SetValue("F_SD_SQR", sqr, -1);
                    billView.InvokeFieldUpdateService("F_SD_SQR", -1);

                    billView.Model.SetItemValueByNumber("F_SD_YG", yg, -1);
                    billView.InvokeFieldUpdateService("F_SD_YG", -1);

                    billView.Model.SetValue("F_SD_LXDH", yglxdh, -1);
                    billView.InvokeFieldUpdateService("F_SD_LXDH", -1);

                    billView.Model.SetValue("F_SD_BFSQ", bfsx, -1);
                    billView.InvokeFieldUpdateService("F_SD_BFSQ", -1);

                    billView.Model.SetValue("F_SD_QSRQ", qsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_QSRQ", -1);

                    billView.Model.SetValue("F_SD_JSRQ", jsdate, -1);
                    billView.InvokeFieldUpdateService("F_SD_JSRQ", -1);

                    var entryRows = data.GetValue<Kingdee.BOS.JSON.JSONArray>("EntryDatas", new Kingdee.BOS.JSON.JSONArray());
                    int i = 0;
                    foreach (Kingdee.BOS.JSON.JSONObject entryRow in entryRows)
                    {
                        var ryxm = entryRow.GetValue<string>("F_SD_RYXM", "");
                        var zjlx = entryRow.GetValue<Int64>("F_SD_ZJLX", 0L);
                        var zjhm = entryRow.GetValue<string>("F_SD_ZJHM", "");
                        var wlrylxdh = entryRow.GetValue<string>("F_SD_LXDHH", "");
                        var rs = entryRow.GetValue<Int64>("F_SD_RS", 0L);
                        var cph = entryRow.GetValue<string>("F_SD_CPH", "");
                        var bfsy = entryRow.GetValue<string>("F_SD_BFSY", "");
                        var clrc = entryRow.GetValue<string>("F_SD_CLRC", "");
                        var cz = entryRow.GetValue<string>("F_SD_CZ", "");
                        var rylx = data.GetValue<Int64>("F_SD_RYLX", 0L);
                        var bz = entryRow.GetValue<string>("F_SD_BZ", "");

                        //var qty = entryRow.GetValue<Decimal>("Qty", 0M);
                        //var note = entryRow.GetValue<string>("Note", "");
                        billView.Model.SetValue("F_SD_RYXM", ryxm, i);
                        billView.InvokeFieldUpdateService("F_SD_RYXM", i);
                        billView.Model.SetValue("F_SD_ZJLX", zjlx, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJLX", i);
                        billView.Model.SetValue("F_SD_ZJHM", zjhm, i);
                        billView.InvokeFieldUpdateService("F_SD_ZJHM", i);
                        billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, i);
                        billView.InvokeFieldUpdateService("F_SD_LXDHH", i);
                        billView.Model.SetValue("F_SD_RS", rs, i);
                        billView.InvokeFieldUpdateService("F_SD_RS", i);
                        billView.Model.SetValue("F_SD_CPH", cph, i);
                        billView.InvokeFieldUpdateService("F_SD_CPH", i);
                        billView.Model.SetValue("F_SD_BFSY", bfsy, i);
                        billView.InvokeFieldUpdateService("F_SD_BFSY", i);
                        billView.Model.SetValue("F_SD_CLRC", clrc, i);
                        billView.InvokeFieldUpdateService("F_SD_CLRC", i);
                        billView.Model.SetValue("F_SD_CZ", cz, i);
                        billView.InvokeFieldUpdateService("F_SD_CZ", i);
                        billView.Model.SetValue("F_SD_RYLX", rylx, i);
                        billView.InvokeFieldUpdateService("F_SD_RYLX", i);
                        billView.Model.SetValue("F_SD_BZ", bz, i);
                        billView.InvokeFieldUpdateService("F_SD_BZ", i);
                        i++;

                        billView.Model.CreateNewEntryRow("FEntity");
                    }


                    //billView.Model.SetValue("F_SD_RYXM", ryxm, 0);
                    //billView.Model.SetValue("F_SD_ZJLX", zjlx, 0);
                    //billView.Model.SetValue("F_SD_ZJHM", zjhm, 0);
                    //billView.Model.SetValue("F_SD_LXDHH", wlrylxdh, 0);
                    //billView.Model.SetValue("F_SD_RS", rs, 0);
                    //billView.Model.SetValue("F_SD_CPH", cph, 0);
                    //billView.Model.SetValue("F_SD_BFSY", bfsy, 0);
                    //billView.Model.SetValue("F_SD_CLRC", clrc, 0);
                    //billView.Model.SetValue("F_SD_CZ", cz, 0);
                    //billView.Model.SetValue("F_SD_RYLX", rylx, 0);
                    //billView.Model.SetValue("F_SD_BZ", bz, 0);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "出场记录单单保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }








                else if (formId == "BDI_GCXM_SGRZ")
                {
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("OrgId", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");
                    var userId = data.GetValue<Int64>("UserId", 0L);
                    var gcxmId = data.GetValue<Int64>("GCXMId", 0L);
                    var xmjdzt = data.GetValue<Int64>("XMJDZTId", 0L);
                    if (gcxmId == 0L) throw new Exception("工程项目不允许为空");
                    var date = data.GetValue<DateTime>("Date", DateTime.Now);
                    var sgbzName = data.GetValue<string>("SGBZName", "");
                    var jlr = data.GetValue<string>("JLRName", "");
                    var tqzk1 = data.GetValue<string>("TQZK1", "");
                    var fl1 = data.GetValue<string>("FL1", "");
                    var qw1 = data.GetValue<string>("QW1", "");
                    var tqzk2 = data.GetValue<string>("TQZK2", "");
                    var fl2 = data.GetValue<string>("FL2", "");
                    var qw2 = data.GetValue<string>("QW2", "");
                    var scqkjl = data.GetValue<string>("SCQKJL", "");
                    var zlaqjl = data.GetValue<string>("ZLAQJL", "");
                    var clgpjjc = data.GetValue<string>("CLGPJJC", "");
                    var remark = data.GetValue<string>("Remark", "");

                    billView.Model.SetValue("FDate", date, -1);
                    billView.InvokeFieldUpdateService("FDate", -1);

                    billView.Model.SetValue("FXMJDZT", xmjdzt, -1);
                    billView.InvokeFieldUpdateService("FXMJDZT", -1);

                    billView.Model.SetValue("FGCXMNumber", gcxmId, -1);
                    billView.InvokeFieldUpdateService("FGCXMNumber", -1);

                    billView.Model.SetValue("FCreatorId", userId, -1);
                    billView.InvokeFieldUpdateService("FCreatorId", -1);

                    billView.Model.SetValue("FSGBZName", sgbzName, -1);
                    billView.InvokeFieldUpdateService("FSGBZName", -1);

                    billView.Model.SetValue("FJLR", jlr, -1);
                    billView.InvokeFieldUpdateService("FJLR", -1);

                    billView.Model.SetValue("FTQZK1", tqzk1, -1);
                    billView.Model.SetValue("FFL1", fl1, -1);
                    billView.Model.SetValue("FQW1", qw1, -1);
                    billView.Model.SetValue("FTQZK2", tqzk2, -1);
                    billView.Model.SetValue("FFL2", fl2, -1);
                    billView.Model.SetValue("FQW2", qw2, -1);
                    billView.Model.SetValue("FSCQKJL", scqkjl, -1);
                    billView.Model.SetValue("FZLAQJL", zlaqjl, -1);
                    billView.Model.SetValue("FCLGPJJC", clgpjjc, -1);
                    billView.Model.SetValue("FRemark", remark, -1);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        #region 上传图片
                        var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        if (pictures.Count > 0)
                        {
                            UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        }
                        #endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "施工日志保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }
                else if (formId == "BDI_GCXM_XXJD")
                {
                    status = data.GetValue<string>("DocumentStatus", "");
                    if (status == "C") throw new Exception("单据已审核，禁止修改");

                    var orgId = data.GetValue<Int64>("OrgId", 1L);

                    if (ctx.CurrentOrganizationInfo.ID != orgId)
                    {
                        ctx.CurrentOrganizationInfo = GetOrganizationInfo(ctx, orgId);
                    }
                    billId = data.GetValue<Int64>("BillId", 0L);
                    billView = GetTargetFormView(ctx, formId, billId);
                    if (billView == null) throw new Exception("单据视图为空");
                    var userId = data.GetValue<Int64>("UserId", 0L);
                    var gcxmId = data.GetValue<Int64>("GCXMId", 0L);
                    if (gcxmId == 0L) throw new Exception("工程项目不允许为空");
                    var date = data.GetValue<DateTime>("Date", DateTime.Now);
                    var supplierId = data.GetValue<Int64>("SupplierId", 0L);
                    var tbrId = data.GetValue<Int64>("TBRId", 0L);
                    var jdRate = data.GetValue<decimal>("LJJDRate", 0M);
                    var remark = data.GetValue<string>("Remark", "");
                    var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                    var picture = "";
                    if (pictures.Count > 0)
                    {
                        picture = Convert.ToString(pictures[0]);
                    }

                    billView.Model.SetValue("FDate", date, -1);
                    billView.InvokeFieldUpdateService("FDate", -1);

                    billView.Model.SetValue("FGCXMID", gcxmId, -1);
                    billView.InvokeFieldUpdateService("FGCXMID", -1);

                    billView.Model.SetValue("FSupplierID", supplierId, -1);
                    billView.InvokeFieldUpdateService("FSupplierID", -1);

                    billView.Model.SetValue("FTBR", tbrId, -1);
                    billView.InvokeFieldUpdateService("FTBR", -1);

                    billView.Model.SetValue("FJDRate", jdRate, -1);
                    billView.InvokeFieldUpdateService("FJDRate", -1);

                    billView.Model.SetValue("FCreatorId", userId, -1);
                    billView.InvokeFieldUpdateService("FCreatorId", -1);

                    billView.Model.SetValue("FRemark", remark, -1);

                    billView.Model.SetValue("FImage", picture, -1);
                    billView.InvokeFieldUpdateService("FImage", -1);

                    var saveOpt = OperateOption.Create();
                    saveOpt.SetVariableValue("ValidatePermission", false);
                    saveOpt.SetIgnoreWarning(true);
                    saveOpt.SetIgnoreScopeValidateFlag(true);
                    saveOpt.AddInteractionFlag(Kingdee.K3.Core.SCM.SCMConst.MinusCheckSensor);// 跳过负库存校验
                    saveOpt.AddInteractionFlag("BillSaveSNCountSpensor");// 跳过序列号校验
                    var retSave = billView.Model.Save(saveOpt);
                    if (retSave.IsSuccess)
                    {
                        var obj = retSave.SuccessDataEnity.FirstOrDefault();
                        billId = Convert.ToInt64(obj["Id"]);
                        var billNO = Convert.ToString(obj["BillNO"]);
                        //#region 上传图片
                        //var pictures = data.GetValue<JSONArray>("Pictures", new JSONArray());
                        //if (pictures.Count > 0)
                        //{
                        //    UploadPictures(ctx, formId, billId, billNO, pictures.Select(p => Convert.ToString(p)).ToArray(), "施工日志图片");
                        //}
                        //#endregion

                        result.Result["Id"] = billId;
                        result.Successful = true;
                        result.Message = retSave.OperateResult.Select(p => p.Message).FirstOrDefault();
                        if (status != "B")
                        {
                            SubmitWorkflow(ctx, formId, billId);
                        }
                    }
                    else
                    {
                        var errorMsg = "形象进度保存失败";
                        if (retSave.ValidationErrors != null && retSave.ValidationErrors.Count > 0)
                        {
                            errorMsg = retSave.ValidationErrors[0].Message;
                        }
                        else if (retSave.OperateResult != null && retSave.OperateResult.Count > 0)
                        {
                            errorMsg = retSave.OperateResult[0].Message;
                        }

                        result.Successful = false;
                        result.Message = errorMsg;
                    }
                }
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }

            return result;
        }
        public Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo AuditBill(string formId, object pkValue)
        {
            var result = new Kingdee.BOS.ServiceFacade.KDServiceEntity.ResultInfo();
            if (pkValue.IsNullOrEmptyOrWhiteSpace())
            {
                result.Successful = false;
                result.Message = "单据内码为空";
                return result;
            }

            try
            {
                var ctx = base.KDContext.Session.AppContext;
                var targetFormMatadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;
                var form = targetFormMatadata.BusinessInfo.GetForm();

                var submitOpt = OperateOption.Create();
                BusinessDataServiceHelper.Submit(ctx, targetFormMatadata.BusinessInfo, new object[] { pkValue }, "Submit", submitOpt);

                var optItem = new List<KeyValuePair<object, object>>() { new KeyValuePair<object, object>(pkValue, "") };
                var auditOpt = OperateOption.Create();
                auditOpt.SetVariableValue("IgnoreWarning", true);
                var retAudit = BusinessDataServiceHelper.SetBillStatus(ctx,
                        targetFormMatadata.BusinessInfo,
                        optItem,
                        new List<object> { "1", "" },
                        "Audit",
                       auditOpt);
                if (retAudit.IsSuccess)
                {
                    result.Successful = true;
                }
                else
                {
                    var errorMsg = string.Format("{0}审核失败", form.Name.ToString());
                    if (retAudit.ValidationErrors != null && retAudit.ValidationErrors.Count > 0)
                    {
                        errorMsg = retAudit.ValidationErrors[0].Message;
                    }
                    else if (retAudit.OperateResult != null && retAudit.OperateResult.Count > 0)
                    {
                        errorMsg = retAudit.OperateResult[0].Message;
                    }

                    result.Successful = false;
                    result.Message = errorMsg;
                    if (errorMsg != null && errorMsg.Contains("已经审核"))
                    {
                        result.Successful = true;
                    }
                }
            }
            catch (Exception e)
            {
                result.Successful = false;
                result.Message = e.Message;
            }

            return result;
        }
        OrganizationInfo GetOrganizationInfo(Context ctx, Int64 orgId)
        {
            OrganizationInfo organizationInfo = null;
            try
            {
                FormMetadata orgMetadata = FormMetaDataCache.GetCachedFormMetaData(ctx, "ORG_Organizations");
                DynamicObject orgDataEntity = BusinessDataServiceHelper.LoadFromCache(ctx, orgMetadata.BusinessInfo.GetDynamicObjectType(), new QueryBuilderParemeter()
                {
                    FormId = "ORG_Organizations",
                    FilterClauseWihtKey = string.Format("FORGID = {0} ", orgId),
                }).FirstOrDefault();

                organizationInfo = new OrganizationInfo()
                {
                    FunctionIds = Convert.ToString(orgDataEntity["OrgFunctions"]).Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Where(p => !string.IsNullOrWhiteSpace(p)).Select(o => Convert.ToInt64(o)).ToList(),
                    ID = Convert.ToInt64(orgDataEntity["Id"]),
                    Name = Convert.ToString(orgDataEntity["Name"]),
                };
            }
            catch (Exception e)
            {

            }

            return organizationInfo;
        }

        public IBillView GetTargetFormView(Context ctx, string targetFormId, object pkId, string billTypeId = "")
        {
            var formMetaData = (FormMetadata)MetaDataServiceHelper.Load(ctx, targetFormId, true);

            if (pkId == null) pkId = 0L;
            var status = OperationStatus.EDIT;
            var pkIdStr = pkId.ToString();
            if (string.IsNullOrWhiteSpace(pkIdStr) || pkIdStr == "0")
            {
                status = OperationStatus.ADDNEW;
            }

            BusinessInfo businessInfo = formMetaData.BusinessInfo;
            Form form = businessInfo.GetForm();
            IResourceServiceProvider formServiceProvider = form.GetFormServiceProvider(false);
            BillOpenParameter billOpenParameter = new BillOpenParameter(targetFormId, null);
            billOpenParameter.SetCustomParameter("formID", targetFormId);
            billOpenParameter.SetCustomParameter("pk", pkId);
            billOpenParameter.SetCustomParameter("status", status == OperationStatus.EDIT ? "EDIT" : "ADDNEW");
            billOpenParameter.SetCustomParameter("PlugIns", businessInfo.GetForm().CreateFormPlugIns());
            billOpenParameter.Context = ctx;
            billOpenParameter.FormMetaData = formMetaData;
            billOpenParameter.LayoutId = billOpenParameter.FormMetaData.GetLayoutInfo().Id;
            billOpenParameter.PkValue = pkId;
            billOpenParameter.Status = status;
            if (string.IsNullOrWhiteSpace(billTypeId) == false)
            {
                billOpenParameter.DefaultBillTypeId = billTypeId;
            }
            object service = formServiceProvider.GetService(typeof(IDynamicFormView));
            (service as IBillViewService).Initialize(billOpenParameter, formServiceProvider);
            (service as IBillView).OpenParameter.Status = status;
            (service as IBillViewService).LoadData();
            (service as IBillView).CommitNetworkCtrl();

            return service as IBillView;
        }
        private void UploadPictures(Context ctx, string formId, Int64 billId, string billNo, string[] pictures, string title = "图片")
        {
            try
            {
                var fileStorgaeType = Kingdee.BOS.ServiceHelper.FileServer.FileServerHelper.GetFileStorgaeType(ctx);
                var tempFilePath = HttpContext.Current.Server.MapPath(KeyConst.TEMPFILEPATH);
                for (var index = 0; index < pictures.Length; index++)
                {
                    var picture = pictures[index];
                    var fileName = string.Format("{0}{1}_{2}_{3}.jpg", title, index + 1, billNo, DateTime.Now.ToString("yyyyMMddHHmmss"));

                    //下载至应用服务器的文件，建议放 WebSite/tempFilePath 目录，开启执行计划后定时会清空
                    var filePath = System.IO.Path.Combine(tempFilePath, fileName);

                    byte[] byteImage = Convert.FromBase64String(picture);
                    System.IO.File.WriteAllBytes(filePath, byteImage);

                    var fileUploadResult = UploadAttachment(ctx, filePath);
                    SaveAttachmentData(ctx, formId, billId, billNo, fileStorgaeType.ToString(), fileUploadResult);
                }
            }
            catch (Exception e)
            {
                Kingdee.BOS.Log.Logger.Error("小程序", title, e);
            }
        }
        private FileUploadResult UploadAttachment(Context ctx, string filePath)
        {
            var uploadResult = new FileUploadResult();
            long blockSize = 1024 * 1024;   //分块上传大小
            string fileName = System.IO.Path.GetFileName(filePath);

            //获取上传下载服务。
            IUpDownloadService service = FileServiceContainer.GetUpDownloadService();
            var tFile = new Kingdee.BOS.FileServer.Core.Object.TFileInfo()
            {
                FileId = string.Empty, //文件编码
                FileName = fileName, //文件名
                CTX = ctx, //登录上下文环境
            };
            using (var fsRead = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite))
            {
                if (fsRead.Length > 0)
                {
                    while (true)
                    {
                        //放里面声明，第一次填入完整字节数据，第二次若只填充一部分，此时后面的直接数组并不会被清空。
                        byte[] content = new byte[blockSize];
                        int size = fsRead.Read(content, 0, content.Length);
                        if (size <= 0) break;

                        //不相等时说明已经是最后一次上传，也可能文件大小刚好等于1M，后面再读时size为0，不会再次上传。
                        tFile.Last = (size != blockSize);
                        tFile.Stream = new System.IO.MemoryStream(content, 0, size);
                        uploadResult = service.UploadAttachment(tFile);
                        tFile.FileId = uploadResult.FileId;

                        if (uploadResult.Success == false || tFile.Last)
                        {
                            break;
                        }
                    }
                }
            }

            return uploadResult;
        }

        private void SaveAttachmentData(Context ctx, string formId, object id, string billNo, string fileStorgaeType, FileUploadResult uploadResult)
        {
            //构建附件明细实体数据包。
            FormMetadata meta = FormMetaDataCache.GetCachedFormMetaData(ctx, FormIdConst.BOS_Attachment);
            DynamicObject dynObj = new DynamicObject(meta.BusinessInfo.GetDynamicObjectType());
            dynObj["BillType"] = formId; //业务单据唯一标识：此处关联币别。
            dynObj["InterID"] = id; //业务单据内码：此处上传至人民币，内码为1。
            dynObj["BillNo"] = billNo; //业务单据编码：此处上传至人民币，编码为PRE001。
            dynObj["EntryKey"] = " ";  //关联实体标识：单据头为空格，单据体则填单据体标识。
            dynObj["EntryInterID"] = -1; //单据体内码：单据头为-1，单据体则填单据体内码。
            dynObj["FileId"] = uploadResult.FileId; //文件编码：上面上传成功拿到文件编码。
            dynObj["FileStorage"] = fileStorgaeType; //存储类型：1为文件服务器、2为亚马逊云存储、3为金蝶·个人云存储、4为金蝶·企业云存储。
            dynObj["AttachmentName"] = uploadResult.FileName; //附件名。
            dynObj["ExtName"] = System.IO.Path.GetExtension(uploadResult.FileName); //文件后缀名
            dynObj["AttachmentSize"] = Math.Round(uploadResult.FileSize * 1.0 / 1024, 2); //附件大小，单位为KB
            dynObj["FBillStatus"] = "A"; //单据状态：给默认值A即可
            dynObj["AliasFileName"] = ""; //别名
            dynObj["IsAllowDownLoad"] = false; //是否禁止下载：false代表允许下载
            dynObj["CreateMen_Id"] = ctx.UserId; //创建人
            dynObj["CreateTime"] = DateTime.Now; //创建时间
            dynObj["ModifyMen_Id"] = ctx.UserId; //修改人
            dynObj["ModifyTime"] = DateTime.Now; //修改时间

            //调用保存接口。
            dynObj = BusinessDataServiceHelper.Save(ctx, dynObj);
        }

        private void SubmitWorkflow(Context ctx, string formId, Int64 billId)
        {
            var targetFormMatadata = MetaDataServiceHelper.Load(ctx, formId) as FormMetadata;

            // 首先判断单据是否已经有未完成的工作流
            bool isExist = ProcManageServiceHelper.CheckUnCompletePrcInstExsit(ctx, formId, billId.ToString());
            if (isExist == true)
            {
                throw new KDBusinessException("AutoSubmit-001", "该单据已经启动了流程，不允许重复提交！");
            }

            // 设置提交参数：忽略操作过程中的警告，避免与用户交互
            OperateOption submitOption = OperateOption.Create();
            submitOption.SetIgnoreWarning(true);
            submitOption.SetIgnoreScopeValidateFlag(true);
            submitOption.SetVariableValue("ValidatePermission", false);
            IOperationResult submitResult = null;

            // 读取单据的工作流配置模板
            var findProcResultList = WorkflowTemplateServiceHelper.GetPrcListByFormID(formId, new string[] { billId.ToString() }, ctx);
            if (findProcResultList == null || findProcResultList.Count == 0)
            {
                var item = new Kingdee.BOS.Workflow.Models.Template.FindPrcResult();
                item.Result = Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.OnlySubmit;
                findProcResultList = new List<Kingdee.BOS.Workflow.Models.Template.FindPrcResult>() { item };
            }

            var findProcResult = findProcResultList.FirstOrDefault();
            if (findProcResult.Result == Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.Error)
            {
                throw new KDBusinessException("AutoSubmit-003", "单据不符合流程启动条件，不允许提交工作流！");
            }
            else if (findProcResult.Result != Kingdee.BOS.Workflow.Models.EnumStatus.TemplateResultType.Normal)
            {
                // 本单无适用的流程图，直接走传统审批
                submitResult = BusinessDataServiceHelper.Submit(ctx, targetFormMatadata.BusinessInfo, new object[] { billId }, "Submit", submitOption);
            }
            else
            {
                // 走工作流
                submitResult = BOSWorkflowServiceHelper.ListSubmit(ctx, targetFormMatadata.BusinessInfo, 0, new object[] { billId }, findProcResultList, submitOption);
            }

            // 判断提交结果
            if (submitResult.IsSuccess == true)
            {
                // 自动提交成功，显示空操作本身的成功提示即可
            }
            else
            {
                submitResult.MergeValidateErrors();
                if (submitResult.OperateResult == null)
                {
                    // 未知原因导致提交失败
                    throw new KDBusinessException("AutoSubmit-004", "未知原因导致自动提交失败！");
                }
                else
                {
                    StringBuilder sb = new StringBuilder();
                    sb.AppendLine("自动提交失败，失败原因：");
                    foreach (var operateResult in submitResult.OperateResult)
                    {
                        sb.AppendLine(operateResult.Message);
                    }
                    throw new KDBusinessException("AutoSubmit-005", sb.ToString());
                }
            }
        }

        DynamicObjectCollection IsStockId(Context ctx, Int64 orgId, Int64 stockId)
        {
            DynamicObjectCollection objs = null;

            var sql = @"
SELECT FSTOCKID FROM t_BD_Stock
WHERE FUSEORGID=@orgId AND FSTOCKID=@stockId
";
            var porgId = new SqlParam("@orgId", KDDbType.Int64, orgId);
            var pstockId = new SqlParam("@stockId", KDDbType.Int64, stockId);
            objs = DBServiceHelper.ExecuteDynamicObject(ctx, sql, null, null, System.Data.CommandType.Text, porgId, pstockId);

            return objs;
        }

        string GetPicturesUrl(string imageBase64, string url)
        {
            var picturesUrl = "";

            string fileName = string.Format("{0}.jpg", Guid.NewGuid().ToString());
            fileName = PathUtils.GetValidFileName(fileName);
            var outServerPath = PathUtils.GetServerPath(KeyConst.TEMPFILEPATH, PathUtils.UrlEncode(fileName));
            var filePath = PathUtils.GetPhysicalPath(KeyConst.TEMPFILEPATH, fileName);

            using (FileStream fs = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.Read))
            {
                using (BinaryWriter bw = new BinaryWriter(fs))
                {
                    bw.Write(imageBase64);
                }
            }
            picturesUrl = string.Format("{0}/{1}", url, outServerPath);
            return picturesUrl;
        }

        Int64 GetAQXMId(Context ctx)
        {
            var aqxmId = 0L;
            var sql = @"
SELECT FID FROM BDI_t_GCXM_AQJCXM"
;

            var objs = DBServiceHelper.ExecuteDynamicObject(ctx, sql, null, null, System.Data.CommandType.Text);
            if (objs.Count > 0)
            {
                var obj = objs[0] as DynamicObject;
                aqxmId = Convert.ToInt64(obj["FID"]);
            }
            return aqxmId;
        }

        Int64 GetZLXMId(Context ctx)
        {
            var zlxmId = 0L;
            var sql = @"
SELECT FID FROM BDI_t_GCXM_ZLJCXM"
;

            var objs = DBServiceHelper.ExecuteDynamicObject(ctx, sql, null, null, System.Data.CommandType.Text);
            if (objs.Count > 0)
            {
                var obj = objs[0] as DynamicObject;
                zlxmId = Convert.ToInt64(obj["FID"]);
            }
            return zlxmId;
        }

    }
}
